--[[

  Copyright 2017 Stefano Mazzucco <stefano AT curso DOT re>

  This program is free software: you can redistribute it and/or modify it under
  the terms of the GNU General Public License as published by the Free Software
  Foundation, either version 3 of the License, or (at your option) any later
  version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  details.

  You should have received a copy of the GNU General Public License along with
  this program.  If not, see <https://www.gnu.org/licenses/gpl-3.0.html>.

]]
local table = table
local client = client -- luacheck: ignore

local naughty = require("naughty")
-- Various helpers
local helpers = require("helpers")

local proxy = require("dbus_proxy")
local Proxy = proxy.Proxy
local Bus = proxy.Bus

local MediaPlayer = require("media_player")
local quodlibet = MediaPlayer:new("quodlibet")
local vlc = MediaPlayer:new("vlc")

local function get_info(player)
  -- For info about key/value pairs
  -- https://www.freedesktop.org/wiki/Specifications/mpris-spec/metadata/
  local info = player:info()

  local position = player:position_as_str()

  if info.length then
    position = position .. " / " .. info.length
  end

  return {info.title,
          helpers.concat_non_nil(
            {info.artists,
             info.album,
             info.year,
             position},
            "\n")
  }
end

local function make_notify(player)

  local notification = nil

  local function notify()
    local opacity = 0.87

    if notification then
      naughty.destroy(
        notification,
        naughty.notificationClosedReason.dismissedByCommand
      )
    end

    if player.is_connected then
      local info = get_info(player)
      local title, text = table.unpack(info)
      notification = naughty.notify({
          title = title,
          text = text,
          icon = player.Metadata["mpris:artUrl"],
          icon_size = 64,
          opacity = opacity})
    else
      notification = naughty.notify({
          title = player.name,
          text = "not available",
          opacity = opacity})
    end
  end

  return notify
end

local function make_wrapper(player, method)
  return function()
    if player.is_connected then
      player[method](player)
    else
      local opacity = 0.87
      naughty.notify({
          title = player.name,
          text = "not available",
          opacity = opacity})
    end
  end
end

quodlibet.notify = make_notify(quodlibet)
quodlibet.play_pause = make_wrapper(quodlibet, "PlayPause")
quodlibet.stop = make_wrapper(quodlibet, "Stop")
quodlibet.quit = make_wrapper(quodlibet, "Quit")
quodlibet.previous = make_wrapper(quodlibet, "Previous")
quodlibet.next = make_wrapper(quodlibet, "Next")

vlc.notify = make_notify(vlc)
vlc.play_pause = make_wrapper(vlc, "PlayPause")
vlc.stop = make_wrapper(vlc, "Stop")
vlc.quit = make_wrapper(vlc, "Quit")
vlc.previous = make_wrapper(vlc, "Previous")
vlc.next = make_wrapper(vlc, "Next")

-- Add notifications on song change.
do
  local dbus = Proxy:new(
      {
        bus = Bus.SESSION,
        name = "org.freedesktop.DBus",
        path= "/org/freedesktop/DBus",
        interface = "org.freedesktop.DBus"
      }
  )

  dbus:connect_signal(
    function(_, name)
      local PREVIOUS_TITLE
      if name == quodlibet.name and quodlibet.is_connected then
        quodlibet:on_properties_changed(function (_, changed)

            if changed.Metadata then
              local current_title = changed.Metadata["xesam:title"]
              if current_title ~= PREVIOUS_TITLE then
                -- Avoids firing up the notification twice.
                quodlibet.notify()
                PREVIOUS_TITLE = current_title
              end

            end
        end)
      end
    end,
    "NameOwnerChanged")
end

return {quodlibet = quodlibet,
        vlc = vlc}
